"""
    读取数据的类
"""
import json
from data_define import Record


# 先定义抽象类
class FileReader:

    def read_data(self) -> list[Record]:
        """读取文件的数据，读到的每一条数据都转换为Record对象，将它们都封装到list内返回即可"""
        pass


# 读取text数据
class TextFileReader:
    def __init__(self, path):
        self.path = path

    # 复写（实现抽象方法）父类的方法

    def read_data(self) -> list[Record]:
        f = open(self.path, "r", encoding="UTF-8")

        record_list: list[Record] = []
        for line in f.readlines():
            line = line.strip()  # 消除读取到的每一行数据中的\n
            data_list = line.split(",")
            record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
            record_list.append(record)

        f.close()
        return record_list


# 读取json数据
class JsonFileReader:
    def __init__(self, path):
        self.path = path

    def read_data(self) -> list[Record]:
        f = open(self.path, 'r', encoding="UTF-8")
        lines = f.readlines()
        record_list: list[Record] = []

        for line in lines:
            data_dict = json.loads(line)
            record = Record(data_dict['date'], data_dict['order_id'], int(data_dict['money']), data_dict['province'], )
            record_list.append(record)
        f.close()
        return record_list


if __name__ == '__main__':
    text_line_data = TextFileReader(
        '/Users/yanfeixingsan/Documents/进阶之路/学习资料/python/第13章资料/2011年1月销售数据.txt')
    json_line_data = JsonFileReader(
        '/Users/yanfeixingsan/Documents/进阶之路/学习资料/python/第13章资料/2011年2月销售数据JSON.txt')
    for l in text_line_data.read_data():
        print(l)

    json_line_data.read_data()
